package rexsee.dialog;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;

public class RexseeImageDialog implements JavascriptInterface {

	public static final String INTERFACE_NAME = "ImageDialog";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeImageDialog(childBrowser);
	}

	public static final String EVENT_ONIMAGEDIALOGDISMISSED = "onImageDialogDismissed";
	public static final String EVENT_ONIMAGEDRAGEDOVERTHRESHOLD = "onImageDragedOverThreshold";

	private final Context mContext;
	private final Browser mBrowser;
	private final HashMap<String, ImageDialog> mMap;
	private boolean syncTag = false;

	public RexseeImageDialog(Browser browser) {
		super();
		mBrowser = browser;
		mContext = browser.getContext();
		mMap = new HashMap<String, ImageDialog>();
		mBrowser.eventList.add(EVENT_ONIMAGEDIALOGDISMISSED);
		mBrowser.eventList.add(EVENT_ONIMAGEDRAGEDOVERTHRESHOLD);
	}

	//JavaScript Interface 
	public void show(final String id, final String url, final String style) {
		if (mMap.containsKey(id)) return;
		syncTag = false;
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					ImageDialog dialog = new ImageDialog(mBrowser, url, style, new ImageMultiTouchListener.OnImageDragedOverThresholdListener() {
						@Override
						public void dragedOverThreshold(int direction) {
							String directionStr;
							switch (direction) {
								case ImageMultiTouchListener.OnImageDragedOverThresholdListener.DIRECTION_UP :
									directionStr = "up";
									break;
								case ImageMultiTouchListener.OnImageDragedOverThresholdListener.DIRECTION_DOWN :
									directionStr = "down";
									break;
								case ImageMultiTouchListener.OnImageDragedOverThresholdListener.DIRECTION_LEFT :
									directionStr = "left";
									break;
								case ImageMultiTouchListener.OnImageDragedOverThresholdListener.DIRECTION_RIGHT :
									directionStr = "right";
									break;
								default :
									directionStr = "unknown";
									break;
							}
							mBrowser.eventList.run(EVENT_ONIMAGEDRAGEDOVERTHRESHOLD, new String[]{id, directionStr});
						}
					});
					mMap.put(id, dialog);
					dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
						@Override
						public void onDismiss(DialogInterface dialog) {
							mMap.remove(id);
							mBrowser.eventList.run(EVENT_ONIMAGEDIALOGDISMISSED, new String[]{id});
						}
					});
					dialog.start();
				} catch (Exception e) {
					mBrowser.exception(getInterfaceName(), e);
				}
				syncTag = true;
			}
		});
		while (!syncTag) {
			try {
				Thread.sleep(100);
			} catch (Exception e) {

			}
		}
		return;
	}
	public void dismiss(String id) {
		if (!mMap.containsKey(id)) return;
		ImageDialog dialog = mMap.get(id);
		if (dialog != null) dialog.dismiss();
		mMap.remove(id);
	}
	public void dismissAll() {
		Set<String> keys = mMap.keySet();
		Iterator<String> iterator = keys.iterator();
		while (iterator.hasNext()) {
			String key = iterator.next();
			ImageDialog dialog = mMap.get(key);
			if (dialog != null) dialog.dismiss();
		}
		mMap.clear();
	}
	public void setStyle(final String id, final String style) {
		if (!mMap.containsKey(id)) return;
		syncTag = false;
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				try {
					mMap.get(id).setStyle(style);
				} catch (Exception e) {
					mBrowser.exception(getInterfaceName(), e);
				}
				syncTag = true;
			}
		});
		while (!syncTag) {
			try {
				Thread.sleep(100);
			} catch (Exception e) {

			}
		}
		return;
	}
	public String getStyle(String id, String name) {
		return (!mMap.containsKey(id)) ? "" : mMap.get(id).getStyle(name);
	}
	public boolean exists(String id) {
		return mMap.containsKey(id);
	}
	public int size() {
		return mMap.size();
	}
	public String getIds() {
		String rtn = "";
		Set<String> ids = mMap.keySet();
		Iterator<String> iterator = ids.iterator();
		while (iterator.hasNext()) {
			if (!rtn.equals("")) rtn += ",";
			rtn += "\"" + iterator.next() + "\"";
		}
		return "[" + rtn + "]";
	}

	public void loadImage(String id, final String url) {
		if (!mMap.containsKey(id)) return;
		final ImageDialog dialog = mMap.get(id);
		if (dialog == null) return;
		syncTag = false;
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				dialog.setImage(url);
				syncTag = true;
			}
		});
		while (!syncTag) {
			try {
				Thread.sleep(100);
			} catch (Exception e) {

			}
		}
	}
	public void clearImage(String id) {
		if (!mMap.containsKey(id)) return;
		final ImageDialog dialog = mMap.get(id);
		if (dialog == null) return;
		syncTag = false;
		((Activity) mContext).runOnUiThread(new Runnable() {
			@Override
			public void run() {
				dialog.setImage(null);
				syncTag = true;
			}
		});
		while (!syncTag) {
			try {
				Thread.sleep(100);
			} catch (Exception e) {

			}
		}
	}
	public void setClickCallback(String id, final String url) {
		if (!mMap.containsKey(id)) return;
		final ImageDialog dialog = mMap.get(id);
		if (dialog == null) return;
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				mBrowser.function.load(url);
			}
		};
		dialog.setClickCallback(runnable);
	}
	public void clearClickCallback(String id) {
		if (!mMap.containsKey(id)) return;
		final ImageDialog dialog = mMap.get(id);
		if (dialog == null) return;
		dialog.setClickCallback(null);
	}

}
